package com.aiju.cash.ui.good.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aiju.base.fragment.BaseFragment;
import com.aiju.bean.Good;
import com.aiju.bean.GoodsType;
import com.aiju.cash.R;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.GoodApi;
import com.aiju.cash.core.api.GoodTypeApi;
import com.aiju.cash.ui.good.GoodAddActivity;
import com.aiju.cash.ui.good.adapter.GoodManagerAdapter;
import com.aiju.cash.ui.good.adapter.GoodManagerTypeAdapter;
import com.aiju.support.recyclerView.DividerItemDecoration;
import com.aiju.utils.T;
import com.aiju.utils.Util;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-01-09 17:01
 * @version: V1.0
 * @Description: 商品管理
 */
public class GoodManagerFragment extends BaseFragment implements GoodManagerTypeAdapter.OnItemClickListener {

    private RecyclerView recyclerviewCategory;
    private RecyclerView recyclerviewTeams;

    public List<GoodsType> getGoodsTypes() {
        return goodsTypes;
    }

    private List<GoodsType> goodsTypes = new ArrayList<>();

    private int show;
    private GoodsType goodsType;
    private GoodManagerTypeAdapter categoryAdapter;
    private GoodManagerAdapter goodManagerAdapter;

    private View empty;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_good_manager, container, false);
        init(root);
        return root;
    }

    private void init(View root) {
        recyclerviewCategory = (RecyclerView) root.findViewById(R.id.recyclerview_category);
        recyclerviewTeams = (RecyclerView) root.findViewById(R.id.recyclerview_teams);
        empty=root.findViewById(R.id.iv_empty);
        requsetGoodType();
    }

    /**
     * 请求商品类型
     */
    private void requsetGoodType() {
        mDialogHelper.showLoadingProgressDialog();
        new GoodTypeApi().goodTypeList(mUser.getStoreId(), mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mDialogHelper.dismissProgressDialog();
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                mDialogHelper.dismissProgressDialog();
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    goodsTypes = new Gson().fromJson(dataJson.getData().toString(), new TypeToken<List<GoodsType>>() {
                    }.getType());
                    setGoodTypeView(goodsTypes);
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }

    /**
     * 设置商品分类列表
     *
     * @param data
     */
    private void setGoodTypeView(List<GoodsType> data) {
        if (!Util.isEmpty(data)) {
            goodsType = data.get(position);
            goodsType.setSeleted(true);

            categoryAdapter = new GoodManagerTypeAdapter(getActivity(), data);
            categoryAdapter.setOnItemClickListener(this);
            recyclerviewCategory.setLayoutManager(new LinearLayoutManager(getContext()));
            recyclerviewCategory.addItemDecoration(new DividerItemDecoration(getContext(),
                    DividerItemDecoration.VERTICAL_LIST));
            recyclerviewCategory.setAdapter(categoryAdapter);

            requsetGoodList(goodsType.getId());
        } else {
            //T.ToastShort("空数据");
            recyclerviewTeams.setVisibility(View.GONE);
            empty.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 跳转商品详情界面
     *
     * @param goodId
     */
    private void startGoodDetailActivity(String goodId) {
        Intent intent = new Intent(getActivity(), GoodAddActivity.class);
        intent.putExtra("goodId", goodId);
        intent.putExtra("type", 1);
        startActivity(intent);
    }

    /**
     * 设置商品view
     */
    private void setGoodView(List<Good> data) {

        goodsType.setGoodsList(data);

        goodManagerAdapter = new GoodManagerAdapter(getActivity(), goodsType);
        goodManagerAdapter.setDelete(isDelete);
        goodManagerAdapter.setOnItemClickListener(new GoodManagerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                if (!isDelete) {
                    startGoodDetailActivity(goodsType.getGoodsList().get(position).getId());
                }else {
                    categoryAdapter.notifyDataSetChanged();
                }
            }
        });
        recyclerviewTeams.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerviewTeams.addItemDecoration(new DividerItemDecoration(getContext(),
                DividerItemDecoration.VERTICAL_LIST));
        recyclerviewTeams.setAdapter(goodManagerAdapter);

        recyclerviewTeams.setVisibility(View.VISIBLE);
        empty.setVisibility(View.GONE);
        if (Util.isEmpty(data)) {
            recyclerviewTeams.setVisibility(View.GONE);
            empty.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 根据分类id获取商品列表
     *
     * @param goodsTypeId 分类id
     */
    private void requsetGoodList(String goodsTypeId) {
        new GoodApi().goodList(goodsTypeId, "1", "999", mUser.getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                T.ToastNetError();
            }

            @Override
            public void onResponse(String response, int id) {
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    List<Good> goods = new Gson().fromJson(dataJson.getData().toString(), new TypeToken<List<Good>>() {
                    }.getType());
                    setGoodView(goods);
                } else {
                    T.ToastShort(dataJson.getMsg());
                }
            }
        });
    }

    private int position;

    @Override
    public void onItemClick(int position) {
        this.position = position;
        for (int i = 0; i < goodsTypes.size(); i++) {
            GoodsType goodsType = goodsTypes.get(i);
            if (position == i) {
                goodsType.setSeleted(true);
            } else {
                goodsType.setSeleted(false);
            }
        }
        categoryAdapter.notifyDataSetChanged();

        goodsType = goodsTypes.get(position);
        if (Util.isEmpty(goodsType.getGoodsList())) {
            requsetGoodList(goodsType.getId());
        } else {
            setGoodView(goodsType.getGoodsList());
        }
    }

    private boolean isDelete;

    public void setDelete(boolean isDelete) {
        this.isDelete = isDelete;
        goodManagerAdapter.setDelete(isDelete);
        categoryAdapter.setDelete(isDelete);
    }

    public void deleteChange() {
        requsetGoodType();
    }

    public GoodsType getGoodType() {
        return goodsType;
    }

}
